package com.yejiarong.elasticsearch.api.service;

import com.yejiarong.common.tools.HttpResult;
import com.yejiarong.elasticsearch.api.bean.request.DeleteByQueryForm;
import com.yejiarong.elasticsearch.api.bean.request.GetRequestForm;
import com.yejiarong.elasticsearch.api.bean.request.SearchRequestForm;
import com.yejiarong.elasticsearch.api.bean.response.DeleteByQueryResult;
import com.yejiarong.elasticsearch.api.bean.response.SearchPageResult;
import com.yejiarong.elasticsearch.api.config.HeaderInterceptor;
import com.yejiarong.elasticsearch.api.bean.request.SaveRequestForm;
import com.yejiarong.elasticsearch.api.bean.response.DeleteResult;
import com.yejiarong.elasticsearch.api.bean.response.SaveResult;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author YeJR
 */
@FeignClient(value = "elasticsearch-service",contextId = "elasticsearchService",  configuration = HeaderInterceptor.class)
public interface ElasticService {


    /**
     * 文档是否存在
     * @param index 索引
     * @param documentId 文档ID
     * @return 是否存在
     */
    @GetMapping(value = "/elasticsearch/exists/{index}/{documentId}")
    HttpResult<Boolean> documentExists(@PathVariable(value = "index") String index, @PathVariable(value = "documentId") String documentId);

    /**
     * 索引文档
     * @param saveRequestForm 索引请求
     * @return SaveResponse
     */
    @PostMapping(value = "/elasticsearch/index")
    HttpResult<SaveResult> indexDocument(@RequestBody SaveRequestForm saveRequestForm);

    /**
     * 更新文档
     * @param saveRequestForm 索引请求
     * @return SaveResponse
     */
    @PostMapping(value = "/elasticsearch/update")
    HttpResult<SaveResult> updateDocument(@RequestBody SaveRequestForm saveRequestForm);

    /**
     * 删除文档
     * @param index 索引
     * @param documentId 文档ID
     * @return DeleteResponse
     */
    @DeleteMapping(value = "/elasticsearch/delete/{index}/{documentId}")
    HttpResult<DeleteResult> deleteDocument(@PathVariable(value = "index") String index, @PathVariable(value = "documentId") String documentId);

    /**
     * 通过查询条件删除文档
     * @param deleteByQueryForm 查询条件
     */
    @DeleteMapping(value = "/elasticsearch/deleteDocumentByQuery")
    HttpResult<DeleteByQueryResult> deleteDocumentByQuery(@RequestBody DeleteByQueryForm deleteByQueryForm);

    /**
     * 文档ID查询
     * @param getRequestForm 请求
     */
    @PostMapping(value = "/elasticsearch/document/id")
    HttpResult<Map<String, Object>> getDocumentById(@RequestBody GetRequestForm getRequestForm);

    /**
     * 查询文档
     * @param searchRequestForm 查询请求
     * @return HttpResult
     */
    @PostMapping(value = "/elasticsearch/document/search")
    HttpResult<SearchPageResult> searchDocument(@RequestBody SearchRequestForm searchRequestForm);
}
